SERVICEAPP_NAME=crm-schedule-task.jar
SERVICEAPP_DEPHOME=/usr/local/crm-schedule-task
SERVICEAPP_LOGPATH=$SERVICEAPP_DEPHOME/logs
ACTUATOR_PORT=8080
JAVA_CMD="java"

#config for 2C4G
JAVA_OPTS="-Xms1024m -Xmx3012m  \
-XX:+AggressiveOpts \
-XX:+UseBiasedLocking \
-XX:+CMSParallelRemarkEnabled \
-XX:+UseConcMarkSweepGC \
-XX:ParallelGCThreads=2 \
-XX:SurvivorRatio=4 \
-verbose:gc \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-XX:+PrintHeapAtGC \
-Xloggc:$SERVICEAPP_LOGPATH/gc.log \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=$SERVICEAPP_LOGPATH/dump.logs "

pid_flag=0

if [ ! -d "$SERVICEAPP_LOGPATH" ];then
	mkdir $SERVICEAPP_LOGPATH
fi

checktpid(){
	tpid=`cat tpid | awk '{print $1}'`
	tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`
	if [ ${tpid} ]; then
	        pid_flag=1
	else
	        pid_flag=0
	fi
}

start(){
    checktpid

    if [ $pid_flag -ne 0 ];then
        echo "WARN:$SERVICEAPP_NAME already started! Ignoring startup request."
    else
        echo "Starting $SERVICEAPP_NAME ..."
        rm -f tpid
        $JAVA_CMD  -jar $JAVA_OPTS  $SERVICEAPP_DEPHOME/$SERVICEAPP_NAME  > $SERVICEAPP_LOGPATH/$SERVICEAPP_NAME.logs  2>&1 &
         echo $! > tpid
    fi
}

#
stop(){
    checktpid
    if [ $pid_flag -ne 0 ];then
        echo "Stoping $SERVICEAPP_NAME..."
        #kill $tpid
		RESULT=$(curl -X POST 127.0.0.1:$ACTUATOR_PORT/actuator/shutdown)
        sleep 6s
        checktpid
        if [ $pid_flag -ne 0 ];then
        		echo "Stoping use kill -9..."
           	kill -9 $tpid
        else
            echo "$SERVICEAPP_NAME Stopped!"
        fi
    else
		 		 echo "WARN:$SERVICEAPP_NAME is not runing"
    fi
}

#
status(){
    checktpid
    if [ $pid_flag -eq 0 ];then
        echo "$SERVICEAPP_NAME is not runing"
    else
        echo "$SERVICEAPP_NAME is runing"
    fi
}

#
case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'restart')
        stop
	sleep 4
        start
        ;;
    'status')
        status
        ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit 0

